package 面试题09_用两个栈实现队列;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 23:15 2021/2/3
 * @Description：
 */

class CQueue {

    Deque<Integer> stack1;
    Deque<Integer> stack2;

    public CQueue() {
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    }

    public void appendTail(int value) {
        stack1.push(value);
    }

    public int deleteHead() {
        // 弹出时使用堆栈2
        if (stack2.size() == 0) {
            // 将堆栈1中的数值弹出并压入堆栈2，此时堆栈2中元素的弹出顺序即为队列出队顺序
            while (stack1.size() > 0) {
                int value = stack1.pop();
                stack2.push(value);
            }
        }

        if (stack2.size() == 0) {
            return -1;
        } else {
            return stack2.pop();
        }
    }
}

